package ch.app.huawei;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * Created by otp on 2020/12/12.
 */
public class MainSiSheWuRu {

    public static void main(String [] args) {
        charAt();
    }

    public static void charAt() {
        double data = 3.06;
        //利用字符串格式化的方式实现四舍五入,保留1位小数
        String result1 = String.format("%.2f",data);
        String re = String.format("%.2f",3.3121);
        //1代表小数点后面的位数, 不足补0。f代表数据是浮点类型。保留2位小数就是“%.2f”。
        System.out.println(result1);//输出3.0

        //利用BigDecimal来实现四舍五入.保留一位小数
        double result2 = new BigDecimal(data).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        //1代表保留1位小数,保留两位小数就是2
        //BigDecimal.ROUND_HALF_UP 代表使用四舍五入的方式
        System.out.println(result2);//输出3.0


        double num1 = 2;
        double num2 = 2.3;
        double num3 = 2.5;
        double num4 = 2.8;
        double num5 = -2;
        double num6 = -2.3;
        double num7 = -2.5;
        double num8 = -2.8;

        //int取整
        System.out.println("===int 取整===");
        System.out.println((int)num1);
        System.out.println((int)num2);
        System.out.println((int)num3);
        System.out.println((int)num4);
        System.out.println((int)num5);
        System.out.println((int)num6);
        System.out.println((int)num7);
        System.out.println((int)num8);

        //Math.floor,floor地板的意思，就是返回小的值
        System.out.println("\n===Math.floor===");
        System.out.println(Math.floor(num1));
        System.out.println(Math.floor(num2));
        System.out.println(Math.floor(num3));
        System.out.println(Math.floor(num4));
        System.out.println(Math.floor(num5));
        System.out.println(Math.floor(num6));
        System.out.println(Math.floor(num7));
        System.out.println(Math.floor(num8));

        //Math.ceil,ceil天花板的意思，就是返回大的值
        System.out.println("\n===Math.ceil===");
        System.out.println(Math.ceil(num1));
        System.out.println(Math.ceil(num2));
        System.out.println(Math.ceil(num3));
        System.out.println(Math.ceil(num4));
        System.out.println(Math.ceil(num5));
        System.out.println(Math.ceil(num6));
        System.out.println(Math.ceil(num7));
        System.out.println(Math.ceil(num8));

        //Math.rint 四舍五入，返回double值
        //注意.5的时候会取偶数
        System.out.println("\n===Math.rint===");
        System.out.println(Math.rint(num1));
        System.out.println(Math.rint(num2));
        System.out.println(Math.rint(num3));
        System.out.println(Math.rint(num4));
        System.out.println(Math.rint(num5));
        System.out.println(Math.rint(num6));
        System.out.println(Math.rint(num7));
        System.out.println(Math.rint(num8));

        //Math.round,float时返回int值，double时返回long值
        System.out.println("\n===Math.round===");
        System.out.println(Math.round(num1));
        System.out.println(Math.round(num2));
        System.out.println(Math.round(num3));
        System.out.println(Math.round(num4));
        System.out.println(Math.round(num5));
        System.out.println(Math.round(num6));
        System.out.println(Math.round(num7));
        System.out.println(Math.round(num8));

        //DecimalFormat
        //注意.5的时候会取偶数
        System.out.println("\n===DecimalFormat===");
        System.out.println(new DecimalFormat("0").format(num1));
        System.out.println(new DecimalFormat("0").format(num2));
        System.out.println(new DecimalFormat("0").format(num3));
        System.out.println(new DecimalFormat("0").format(num4));
        System.out.println(new DecimalFormat("0").format(num5));
        System.out.println(new DecimalFormat("0").format(num6));
        System.out.println(new DecimalFormat("0").format(num7));
        System.out.println(new DecimalFormat("0").format(num8));

        //setScale(0, BigDecimal.ROUND_HALF_UP))
        System.out.println("\n===setScale(0, BigDecimal.ROUND_HALF_UP))===");
        System.out.println(new BigDecimal(num1).setScale(0, BigDecimal.ROUND_HALF_UP));
        System.out.println(new BigDecimal(num2).setScale(0, BigDecimal.ROUND_HALF_UP));
        System.out.println(new BigDecimal(num3).setScale(0, BigDecimal.ROUND_HALF_UP));
        System.out.println(new BigDecimal(num4).setScale(0, BigDecimal.ROUND_HALF_UP));
        System.out.println(new BigDecimal(num5).setScale(0, BigDecimal.ROUND_HALF_UP));
        System.out.println(new BigDecimal(num6).setScale(0, BigDecimal.ROUND_HALF_UP));
        System.out.println(new BigDecimal(num7).setScale(0, BigDecimal.ROUND_HALF_UP));
        System.out.println(new BigDecimal(num8).setScale(0, BigDecimal.ROUND_HALF_UP));

        //abs求绝对值
        System.out.println("\n===abs求绝对值===");
        System.out.println(Math.abs(num3));
        System.out.println(Math.abs(num7));

    }
}
